package com.shanghaionstar.activity.webview.handler;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.jsBridge.CommonWebView;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.mag.enumtype.CategoryType;
import com.onstar.cn.more.response.GetContentDetailResponseType;
import com.shanghaionstar.R;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by Rengui Lu on 2017/3/3.
 */
public class DetailContentWebHandler extends DefaultWebHandler {
    private CommonWebviewPresenter presenter;

    public DetailContentWebHandler(Context context) {
        super(context);
    }

    @Override
    public void handMsg(Message msg) {
        switch (msg.what) {
            case IWebType.TYPE_CONTENT_ID:
                handleDetailContent(msg);
                break;
            case IWebType.TYPE_CONTENT_DETAIL:
                initContentDetailPage(msg);
                break;
        }
    }

    private void handleDetailContent(Message msg) {
        CommonWebViewActivity mActivity;
        if (context instanceof CommonWebViewActivity) {
            mActivity = (CommonWebViewActivity) context;
            Bundle bundle = mActivity.getBundle();
            TopLayout topLayout = mActivity.getTop_layout();
            String title = ((WebDataEntity) msg.obj).getTopTitle();
            initView(bundle, topLayout, title);
            mActivity.getWebview().setupMySettings();
            presenter = new CommonWebviewPresenter(mActivity.getScheduler());
            presenter.setView(mActivity);
            presenter.getCategoryContentById(bundle);
        }
    }


    public void initView(Bundle bundle, TopLayout topLayout, String title) {
        topLayout.setVisibility(View.VISIBLE);
        CategoryType categoryType = (CategoryType) bundle.getSerializable("categoryType");
        switch (categoryType) {
            case BUICK_CARE_JPFW:
                topLayout.setTopTitle(R.string.content_bar_buick_bestservice);
                break;
            case BUICK_CARE_HDXX:
                topLayout.setTopTitle(R.string.content_bar_buick_activityinfo);
                break;
            case BUICK_CARE_YCTS:
                topLayout.setTopTitle(R.string.content_bar_buick_maintaintips);
                break;
            case ONSTAR_SYBZ:
                LayoutInflater mInflater = LayoutInflater.from(context);
                LinearLayout linearLayout = (LinearLayout) mInflater.inflate(R.layout.help_detail_title, null);
                RelativeLayout relativeLayout = (RelativeLayout) topLayout.getParent();
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                lp.addRule(RelativeLayout.BELOW, topLayout.getId());
                linearLayout.setLayoutParams(lp);
                relativeLayout.addView(linearLayout,lp);
                CommonWebViewActivity mActivity = (CommonWebViewActivity) context;
                CommonWebView webView = mActivity.getWebview();
                RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) webView.getLayoutParams();
                lp1.addRule(RelativeLayout.BELOW, linearLayout.getId());
                webView.setLayoutParams(lp1);
                TextView tv = (TextView) linearLayout.findViewById(R.id.detail_title_tv);
                tv.setText(bundle.getString("detailTitle"));
                topLayout.setTopTitle(title);
                break;
            case TRUE_STORY:
                topLayout.setTopTitle(R.string.content_bar_story);
                break;
            case ASSISTANT:
                topLayout.setTopTitle(R.string.content_bar_assistant);
                break;
            case ONSTAR_FWJS:
                topLayout.setTopTitle(R.string.services_introduction);
                break;
            case ONSTAR_SSA:
                topLayout.setTopTitle(R.string.option_onstar_protocol);
                break;
            default:
                topLayout.setTopTitle(title);
                break;
        }
    }



    private void initContentDetailPage(Message msg) {
        CommonWebViewActivity mActivity;
        if (context instanceof CommonWebViewActivity) {
            mActivity = (CommonWebViewActivity) context;
            WebView webView = mActivity.getWebview();
            GetContentDetailResponseType contentDetailResponse = (GetContentDetailResponseType) msg.obj;
            initContentDetailPage(contentDetailResponse, webView);
        }
    }

    public void initContentDetailPage(GetContentDetailResponseType contentDetailResponse, WebView detailWebView) {
        if (!CommonUtil.isNull(contentDetailResponse.getContent())) {
            String content = contentDetailResponse.getContent().getContent();
            String contentUrl = contentDetailResponse.getContent().getContentUrl();
            if (content != null) {
                detailWebView.loadDataWithBaseURL("file:///android_asset/",
                        content.replaceAll("null", ""), "text/html", "utf-8", "");
            } else if (contentUrl != null) {
                detailWebView.loadUrl(contentUrl);
            }
        }
    }


}
